package com.aifast.system.sys.controller;

import cn.hutool.core.lang.Console;
import com.aifast.common.base.BaseEntity;
import com.aifast.common.base.Controller;
import com.aifast.common.base.R;
import com.aifast.system.sys.entity.Gc;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

/**
 * @Author fujian
 * @since 2022/1/7 9:40
 */
@RestController
@RequestMapping("gc")
public class GenController extends Controller {

    final static String path = "D:/IdeaWorkSpace/mp/";
    final static String ON = "on";

    @PostMapping("go")
    public R go(@RequestBody Gc gc){
        Console.log(gc);
        return generatorCode(gc) ? OK() : ERROR();
    }

    private Boolean generatorCode(Gc gc){
        try {
            FastAutoGenerator.create(setting.getUrl(), setting.getUsername(), setting.getPassword())
                    .globalConfig(builder -> {
                        builder.author(gc.getAuthor()) // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                                .fileOverride() // 覆盖已生成文件
                                .outputDir(path); // 指定输出目录
                    })
                    .packageConfig(builder -> {
                        builder.parent(gc.getPackageName()) // 设置父包名
                                .moduleName(gc.getModuleName()) // 设置父包模块名
                                .mapper("dao").serviceImpl("service").xml("xml")
                                .pathInfo(Collections.singletonMap(OutputFile.entity,path))
                                .pathInfo(Collections.singletonMap(OutputFile.serviceImpl,path))
                                .pathInfo(Collections.singletonMap(OutputFile.mapper,path))
                                .pathInfo(Collections.singletonMap(OutputFile.controller,path))
                                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, path)); // 设置mapperXml生成路径
                    })
                    .strategyConfig(builder -> {
                        builder.addInclude(gc.getTableName()) // 设置需要生成的表名
                                .addTablePrefix("t_", "c_"); // 设置过滤表前缀

                    })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                    .templateConfig(builder -> {
                        builder.disable(TemplateType.CONTROLLER,TemplateType.SERVICEIMPL,TemplateType.SERVICE,TemplateType.ENTITY)
                                .controller("/templates/gc/controller.java")
                                .serviceImpl("/templates/gc/serviceImpl.java")
                                .entity("/templates/gc/entity.java")
                                .build();
                    })
                    .strategyConfig(builder -> {
                        builder.entityBuilder()
                                .enableLombok()
                                .enableChainModel()
//                            .enableActiveRecord()
                                .superClass(BaseEntity.class)
                                .enableRemoveIsPrefix()
                                .addIgnoreColumns("created_by","created_time","update_by","update_time","deleted")
                                .build();

                    })
                    .strategyConfig(builder -> {
                        builder.controllerBuilder()
//                            .enableHyphenStyle()
                                .enableRestStyle()
                                .superClass(Controller.class)
                                .build();
                    })

                    .strategyConfig(builder -> {
                        builder.serviceBuilder()
                                .formatServiceImplFileName("%sService")
                                .build();
                    })

                    .strategyConfig(builder -> {
                        builder.mapperBuilder()
                                .enableMapperAnnotation()
                                .enableBaseResultMap()
                                .enableBaseColumnList()
                                .formatMapperFileName("%sDao")
                                .formatXmlFileName("%s")
                                .build();
                    })
                    .execute();
            return true;
        }catch (Exception e){
            return false;
        }

    }

}
